package app.poller.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ListView;
import app.poller.R;
import app.poller.adapter.ContactArrayAdapter;
import app.poller.adapter.ContactAutoCompleteArrayAdapter;
import app.poller.entities.Contact;

public class AddContactActivity extends Activity {

	AutoCompleteTextView toList;
	ListView senderListView;

	ContactAutoCompleteArrayAdapter contactAutoCompleteSuggestionAdapter;
	ContactArrayAdapter addedContactArrayAdapter;
	
	Button sendButton;
	Button cancelButton;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_send);
		getActionBar().setDisplayHomeAsUpEnabled(true);
		toList = (AutoCompleteTextView) findViewById(R.id.send_autocomplete_textview);
		senderListView = (ListView) findViewById(R.id.sender_contact_list);
		sendButton = (Button) findViewById(R.id.send_button);
		cancelButton = (Button) findViewById(R.id.cancel_button);
		
		sendButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				Intent resultData = new Intent();
				ArrayList<Contact> senderList = new ArrayList<Contact>();
				for(int i = 0; i < addedContactArrayAdapter.getCount(); i++) {
					senderList.add(addedContactArrayAdapter.getItem(i));
				}
				resultData.putParcelableArrayListExtra("result", senderList);
				setResult(Activity.RESULT_OK, resultData);
				finish();
			}
		});
		
		ArrayList<Contact> contactAutoCompleteList = getContactsWithPhoneNumber();
		contactAutoCompleteSuggestionAdapter = new ContactAutoCompleteArrayAdapter(getApplicationContext(), R.layout.contact_autocomplete_row, contactAutoCompleteList);
		
		addedContactArrayAdapter = new ContactArrayAdapter(getApplicationContext(), R.layout.contact_row);
		senderListView.setAdapter(addedContactArrayAdapter);	
		
		toList.setAdapter(contactAutoCompleteSuggestionAdapter);
		toList.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
				addedContactArrayAdapter.add(contactAutoCompleteSuggestionAdapter.getItem(position));
				addedContactArrayAdapter.notifyDataSetChanged();
				toList.setText("");
				Log.e("hello", "executed " + contactAutoCompleteSuggestionAdapter.getItem(position).getContactName());
			}
		});
		
		ArrayList<Contact> contacts = getIntent().getExtras().getParcelableArrayList("contactlist");
		addedContactArrayAdapter.addAll(contacts);
		addedContactArrayAdapter.notifyDataSetChanged();
	}


	private ArrayList<Contact> getContactsWithPhoneNumber() {

		ArrayList<Contact> contactList = new ArrayList<Contact>();
		Cursor cur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[] {Phone._ID, Phone.DISPLAY_NAME, Phone.NUMBER}, null, null, null);

		if (cur.getCount() > 0) {
			while (cur.moveToNext()) {
				String name = cur.getString(cur.getColumnIndex(Phone.DISPLAY_NAME));
				String number = cur.getString(cur.getColumnIndex(Phone.NUMBER));
				contactList.add(new Contact(name, number));
			}
		}
		return contactList;
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_send, menu);
		return true;
	}


	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}
